

// 全部成功才成功，一旦一个失败则失败

function all1(promises) {
  let len = promises.length
  let resulst = [] // 输出结果
  if (!len) return


  return new Promise((resolve, reject) => {
    const fn = (index, val) => {
      resulst[index] = val
      if (resulst.length === len) {
        resolve(resulst)
      }
    }
    for (let i = 0; i < len; i++) {
      let p = promises[i] // 可能是常量，也可能是一个promise方法
      if (p && typeof p.then === 'function') {
        p.then((data) => {
          fn(i, data)
        }, (err) => {
          reject(err)
        })
      } else {
        fn(i, p)
      }
    }
  })
}

all1([1,
  2,
  3,
  // new Promise((resolve, reject) => {
  //   throw new Error('jsjsjsj')
  // }),
  new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(100);
    }, 1000);
  })])
  .then((data) => {
    console.log(data);
  })
  .catch((err) => {
    console.log(err, "失败了");
  });